/*
 *说明：用户组已选择的授权资源脚本
 *
 *@author liuxun 2012-03-26
*/


var opeForm;
$(function() {
	
	opeForm = $('#opeForm');
	hitTree("0");
	$("#reloadBtn").click(refreshWin);
	$("#addBtn").click(addOperation);
	$('#activateBtn').click(activateOperation);
	$('#deactivateBtn').click(deactivateOperation);
	$("#deleteBtn").click(deleteOperation);
	
});

// 新增授权资源操作
function addOperation() {
	var selectedNode = treeFrame.getSelecdedNode();
	
	if (!selectedNode) {
		showInfoWin('请先选中节点!');
		return;
	}
	
	if (selectedNode.id == '0') {
		showInfoWin('不能选择根节点!');
		return;
	}	
	
	var usr_grp_id = selectedNode.attributes.usr_grp_id;
	var url = 'D_ACL!showUsrgrpNotSelectedRsc.do?model.auth_obj_id=' + usr_grp_id;
	openWin(url, 'pop', 'A0', '1150', '750');
}

//激活操作 
function activateOperation() {
	var seletedNode = treeFrame.getSelecdedNode();
	if (seletedNode == null) {
		showInfoWin("请先选择用户组！");
		return;
	}
	
	if(seletedNode.id == '0'){
		showInfoWin('不允许删除根节点!');
		return;
	}
	
	var rows = mainFrame.grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].ACL_ID);
	}
	var params = $ .param({'id' : ids}, true);
	
	var url = 'D_ACL!showUsrGrpReActivate.do?params=' + ids;
	openWin(url, 'pop', 'A0', '1150', '750');
}

//失效操作
function deactivateOperation() {
	var seletedNode = treeFrame.getSelecdedNode();
	if (seletedNode == null) {
		showInfoWin("请先选择用户组！");
		return;
	}
	
	if(seletedNode.id == '0'){
		showInfoWin('不允许删除根节点!');
		return;
	}
	
	var rows = mainFrame.grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		if (rows[i].ACL_STATUS == '02') {
			showInfoWin('选择的记录已经存在失效的记录!');
			return;
		}
		ids.push(rows[i].ACL_ID);
	}
	var params = $ .param({'id' : ids}, true);
	
	$.messager.confirm('提示信息', '您确定要失效选择的用户组授权资源信息吗?', function(result) {
		if (result) {
			showSystemProccessing();
			$.ajax( {
				url : 'D_ACL!doDeactivate.do',
				data : params,
				type : 'post',
				success : function(data) {
					removeSystemProccessing();
					showInfoWin("操作成功！");
					mainFrame.afterOK();
				}
			});
		}
	});
}

// 删除操作
function deleteOperation() {
	var seletedNode = treeFrame.getSelecdedNode();
	if (seletedNode == null) {
		showInfoWin("请先选择用户组！");
		return;
	}
	
	if(seletedNode.id == '0'){
		showInfoWin('不允许删除根节点!');
		return;
	}
	
	var rows = mainFrame.grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].ACL_ID);
	}
	var params = $ .param({'id' : ids}, true);
	
	$.messager.confirm('提示信息', '您确定要删除选择的用户组授权资源信息吗?', function(result) {
		if (result) {
			showSystemProccessing();
			$.ajax( {
				url : 'D_ACL!doDelete.do',
				data : params,
				type : 'post',
				success : function(data) {
					removeSystemProccessing();
					showInfoWin("操作成功！");
					mainFrame.afterOK();
				}
			});
		}
	});
}

// 处理左侧树的点击事件
function hitTree(code, txt, deep, isleaf, attributes) {
	var url;
	if (code == '0') {
		url = 'D_ACL!showUsrgrpSelectedAuthRsc.do';
	} else { 
		url = 'D_ACL!showUsrgrpSelectedAuthRsc.do?model.auth_obj_id=' + attributes.usr_grp_id;
	}
	$('#mainFrame').attr('src', url);
}
